package com.tensquare.search.controller;

import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.SearchService;
import entity.PageResult;
import entity.Result;

import entity.StatusCode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Pageable;

/**
 * Created with IDEA
 * author:zhiXiang-Chen
 * Date:2019/3/2
 * Time:20:14
 */
@RestController
@RequestMapping("/search")
@CrossOrigin
public class SearchController {

    @Autowired
    private SearchService searchService;

    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Article article){
        searchService.save(article);
        return new Result(true, StatusCode.OK,"增加成功");
    }
    //URL: /search/{keywords}/{page}/{size},   method: GET
    @RequestMapping(value = "/{keywords}/{page}/{size}",method = RequestMethod.GET)
    public Result search(@PathVariable(value = "keywords") String keywords,@PathVariable(value = "page") int page,@PathVariable(value = "size") int size){

       Page<Article> pageList = searchService.search(keywords,page,size);

        PageResult pageResult = new PageResult(pageList.getTotalElements(),pageList.getContent());
        return new Result(true, StatusCode.OK,"查询成功",pageResult);
    }

}
